//
//  SelectTimeViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/6.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class SelectTimeViewController: KMViewController {
    
    fileprivate let cellId = "selectTimeCell"
    fileprivate let dataArray = [once,monthly]
    fileprivate var tableView : UITableView!
    
    var timeBlock : ((String)->())?

    override func viewDidLoad() {
        super.viewDidLoad()

        createTableView()
        
    }
    
    //MARK: 创建tableView
    fileprivate func createTableView() {
        
        let whiteView = UIView()
        whiteView.backgroundColor = UIColor.white
        self.view.addSubview(whiteView)
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.backgroundColor = UIColor.white
        tableView.bounces = false
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(SelectTimeCell.self, forCellReuseIdentifier: cellId)
        whiteView.addSubview(tableView)
        
        whiteView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.height.equalTo(RATIO_H(maxNum: 160))
        }
        
        tableView.snp.makeConstraints { (make) in
            make.left.right.centerY.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 100))
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        self.dismiss(animated: false, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension SelectTimeViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return RATIO_H(maxNum: 50)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? SelectTimeCell
        cell?.titleLabel.text = dataArray[indexPath.row]
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        self.timeBlock!(dataArray[indexPath.row])
        self.dismiss(animated: false, completion: nil)
    }
    
    
}
